package leetcode.editor.cn;

public class P783MinimumDistanceBetweenBstNodes {
    public static void main(String[] args) {
        Solution solution = new P783MinimumDistanceBetweenBstNodes().new Solution();
        TreeNode root = new TreeNode(27);
        root.right = new TreeNode(34);
        root.right.right = new TreeNode(58);
        root.right.right.left = new TreeNode(50);
        root.right.right.left.left = new TreeNode(44);
        System.out.println(solution.minDiffInBST(root));

    }

    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        int min = Integer.MAX_VALUE;

        public int minDiffInBST(TreeNode root) {
            minDiffInBST(root, null);
            return min;
        }

        public void minDiffInBST(TreeNode root,TreeNode preNode) {
            if (root == null) {
                return;
            }
            minDiffInBST(root.left,preNode);
            
            if (preNode != null) {
                min = Math.min(Math.abs(root.val - preNode.val), min);
            }
            preNode = root;
            minDiffInBST(root.right, root);

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}